﻿using LeEdu.DTO;
using LeEdu.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeEdu.DAL.DataAccess
{
    using ServiceStack.OrmLite;

    public class CourseDAL:BaseDAL,ICourseDAL
    {
        public bool AddCourse(Course course)
        {
            using (var cnn = DbConnection)
            {
                var rtv = cnn.Insert<Course>(course);
                return rtv > 0;
            }
        }

        public bool DelCourse(Guid cid)
        {
            using (var cnn = DbConnection)
            {
                var rtv = cnn.DeleteById<Course>(cid);
                return rtv > 0;
            }
        }
        public Course GetCourseById(Guid cid)
        {
            using (var cnn = DbConnection)
            {
                 
                 
                var rtv= cnn.Select<Course>(cnn.From<Course>().Where(m => m.Id == cid)).FirstOrDefault();
                return rtv;
            }  
        }

        public bool EditCourse(Course course)
        {           
            using (var cnn = DbConnection)
            {                
                var rtv = cnn.Update(course);
                return rtv > 0;
            } 
        }
        

        public List<Course> GetCourses(int pageIndex, int pageSize, out long totalSize)
        {
            using (var cnn=DbConnection)
            {
                var sql= cnn.From<Course>()
                    .OrderByDescending(m=>m.CreateTime)
                    .Limit((pageIndex - 1) * pageSize, pageSize);

                totalSize = cnn.Count<Course>();

                return cnn.Select<Course>(sql);
            }
        }

        public List<Course> GetCoursesList()
        {
            using (var cnn = DbConnection)
            {
                var sql = cnn.From<Course>();
                return cnn.Select<Course>(sql);
            }
        }
    }
}
